package com.java2007.hotel.dao.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.dao.OrderDetailDao;
import com.java2007.hotel.pojo.OrderDetail;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/28
 **/
public class OrderDetailDaoImpl implements OrderDetailDao {
    /**
     * 添加订单详情
     * @param orderDetail
     * @return
     * @throws SQLException
     */
    @Override
    public int addorderDetail(OrderDetail orderDetail) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "INSERT INTO t_order_detail (detail_id,detail_food_name,detail_food_id,detail_price,detail_number,detail_total_price,detail_create_time,detail_update_time,detail_order_id)VALUES(?,?,?,?,?,?,?,?,?)";
        return queryRunner.update(JdbcUtils.getConnection(),sql,orderDetail.getDetailId(),orderDetail.getDetailFoodName(),orderDetail.getDetailFoodId(),orderDetail.getDetailPrice(),orderDetail.getDetailNumber(),orderDetail.getDetailTotalPrice(),orderDetail.getDetailCreateTime(),orderDetail.getDetailUpdateTime(),orderDetail.getDetailOrderId());
    }

    /**
     * 通过订单id查询订单详情
     * @param detailOrderId
     * @return
     */
    @Override
    public List<OrderDetail> findOrderDetail(Long detailOrderId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT " +
                "detail_id detailId,detail_food_name detailFoodName,detail_food_id detailFoodId,detail_price detailPrice,detail_number detailNumber,detail_total_price detailTotalPrice,detail_create_time detailCreateTime,detail_update_time detailUpdateTime,detail_order_id detailOrderId " +
                "FROM t_order_detail " +
                "WHERE detail_order_id = ?";
        return queryRunner.query(sql,new BeanListHandler<>(OrderDetail.class),detailOrderId);
    }
}
